package com.kky.pp.activity.match

import android.os.Bundle
import com.kky.pp.common.Constant
import com.kky.pp.callback.EmptySuccessCallBack
import com.kky.pp.popup.MemberBuyPopup
import com.kky.pp.entity.MemberSubscribeEntity
import io.rong.imkit.entity.OrderCreateEntity
import io.rong.imkit.event.EnumEventTag
import com.kky.pp.util.GlideUtil
import com.custom.base.config.BaseConfig
import com.kky.pp.pay.GooglePayUtils
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.mvp.BasePresenterImpl
import com.custom.base.base.SDEventManager
import io.rong.imkit.API
import io.rong.imkit.IMCenter
import io.rong.imkit.SpName
import io.rong.imkit.entity.BaseEntity
import io.rong.imkit.entity.OpenChatEntity
import io.rong.imlib.IRongCallback
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message
import io.rong.message.TextMessage
import kotlin.random.Random


class MatchedPresenter : BasePresenterImpl<MatchedContract.View>(),
    MatchedContract.Presenter {
    var bun :Bundle?=null
    private val textList= arrayListOf("Go ahead and flirt boldly!", "Be bolder in approaching!", "Be even more proactive!")

    override fun initBundle(bundle: Bundle) {
        bun=bundle
        mView?.apply {
            GlideUtil.setImageView(bundle.getString("avatarUrl"),imgViewMatchSuccessful,true)
            GlideUtil.setImageView(bundle.getString("avatarUrl"),imageViewMatchSuccessfulLike)


            val randomIndex = Random.nextInt(textList.size)
            val randomQuote = textList[randomIndex]
            editMatchSuccessful.hint=randomQuote


           val userImg = BaseConfig.getInstance.getString(SpName.avatarUrl,"")
            if (userImg.isNotEmpty()){
                GlideUtil.setImageView(userImg,imageViewMatchSuccessfulOwn)
            }
            SDEventManager.post(EnumEventTag.INDEX_MATCH_REFRESH_DATA.ordinal)

        }
    }
    fun userSend(content:String){
        mView?.apply {
            val targetId = bun!!.getString("userCodeFriend","")

            OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
                override fun addBody(requestBody: OkHttpBodyEntity) {
                    requestBody.setPost(API.user_chat_open_url)
                    requestBody.add("userCodeFriend", targetId)
                    requestBody.add("type", 1)
                }
            }, object : SDOkHttpResoutCallBack<OpenChatEntity>(false) {
                override fun onSuccess(entity: OpenChatEntity) {
                }
            })

            val conversationType: Conversation.ConversationType = Conversation.ConversationType.PRIVATE
            val messageContent = TextMessage.obtain(content)

            val message: Message = Message.obtain(targetId, conversationType, messageContent)


            IMCenter.getInstance()
                .sendMessage(message, null, null, object : IRongCallback.ISendMessageCallback {
                    override fun onAttached(message: Message?) {}
                    override fun onSuccess(message: Message?) {
                        SDEventManager.post(EnumEventTag.INDEX_MATCH_REFRESH_DATA.ordinal)
                        finish()
                    }
                    override fun onError(message: Message?, errorCode: RongIMClient.ErrorCode?) {}
                })
        }
    }
    fun tryToSend(content:String){
        val targetId = bun!!.getString("userCodeFriend","")
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_send_message_url)
                requestBody.add("toUserId", targetId)
            }

        }, object : SDOkHttpResoutCallBack<BaseEntity>() {
            override fun onSuccess(entity: BaseEntity) {
                userSend(content)
            }

            override fun onFailure(code: Int, msg: String) {
                super.onFailure(code, msg)
                when(code){
                    3004,3007,3008,3009->{
                        MemberBuyPopup(
                            mActivity,
                            0,
                            object : MemberBuyPopup.ChangeMembershipListener {
                                override fun onListener(bean: MemberSubscribeEntity.Data.ProductDescriptions) {
                                    OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
                                        override fun addBody(requestBody: OkHttpBodyEntity) {
                                            requestBody.setPost(Constant.user_create_order_url)
                                            requestBody.add("productCode", bean.productCode)
                                            requestBody.add("productCategory", 1)
                                        }
                                    }, object : SDOkHttpResoutCallBack<OrderCreateEntity?>() {

                                        override fun onSuccess(entity: OrderCreateEntity?) {
                                            GooglePayUtils.instance.start(entity as OrderCreateEntity,mActivity,object :
                                                EmptySuccessCallBack {
                                                override fun OnSuccessListener() {
                                                    BaseConfig.getInstance.setBoolean(SpName.isMember, true)

                                                }

                                            })
                                        }
                                    })
                                }

                                override fun closeListener(refreshTime: Long) {
                                }
                            })
                    }
                }

            }
        })
    }

}